import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import '../services/language_service.dart';
import '../services/theme_service.dart';
import '../generated/l10n/app_localizations.dart';
import 'package:flutter/rendering.dart';

class LanguageSettingsScreen extends StatefulWidget {
  const LanguageSettingsScreen({super.key});

  @override
  State<LanguageSettingsScreen> createState() => _LanguageSettingsScreenState();
}

class _LanguageSettingsScreenState extends State<LanguageSettingsScreen> {
  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context)!;
    final theme = Theme.of(context);

    return Consumer<LanguageService>(
      builder: (context, languageService, child) {
        return Scaffold(
          backgroundColor: theme.scaffoldBackgroundColor,
          appBar: AppBar(
            backgroundColor: theme.appBarTheme.backgroundColor,
            elevation: 0,
            leading: IconButton(
              icon: Icon(Icons.arrow_back, color: theme.iconTheme.color),
              onPressed: () => Navigator.pop(context),
            ),
            title: Text(
              l10n.selectLanguage,
              style: GoogleFonts.notoSansSc(
                fontSize: 20,
                fontWeight: FontWeight.w500,
                color: theme.textTheme.titleLarge?.color,
              ),
            ),
            centerTitle: true,
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text(
                  l10n.confirm,
                  style: GoogleFonts.notoSansSc(
                    fontSize: 16,
                    color: theme.colorScheme.primary,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
          body: ListView.builder(
            itemCount: LanguageService.supportedLocales.length,
            padding: const EdgeInsets.symmetric(vertical: 8),
            itemBuilder: (context, index) {
              final locale = LanguageService.supportedLocales[index];
              final isSelected = locale.languageCode ==
                  languageService.currentLocale.languageCode;

              return ListTile(
                onTap: () {
                  languageService.setLocale(locale);
                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(
                        l10n.languageChanged(
                          languageService.getLanguageName(context, locale),
                        ),
                        style: GoogleFonts.notoSansSc(
                          color: Colors.white,
                        ),
                      ),
                      behavior: SnackBarBehavior.floating,
                      backgroundColor: theme.colorScheme.primary,
                      duration: const Duration(seconds: 2),
                    ),
                  );
                },
                contentPadding: const EdgeInsets.symmetric(horizontal: 20),
                title: Text(
                  languageService.getLanguageName(context, locale),
                  style: GoogleFonts.notoSansSc(
                    fontSize: 16,
                    color: theme.textTheme.bodyLarge?.color,
                    fontWeight: isSelected ? FontWeight.w500 : FontWeight.w400,
                  ),
                ),
                trailing: isSelected
                    ? Icon(
                        Icons.check_circle,
                        color: theme.colorScheme.primary,
                        size: 24,
                      )
                    : null,
              );
            },
          ),
        );
      },
    );
  }
}
